<template>
  <div id="list">
    <!-- 学生列表展示 -->
    <el-card shadow="hover">
      <!-- 添加学生信息 -->
      <el-form :inline="true" :model="formInline" ref="formInline" class="demo-form-inline">
        <!-- <el-form-item label="学号" required>
          <el-input v-model="formInline.stuId" placeholder="学号"></el-input>
        </el-form-item> -->                                                              
        <el-form-item label="姓名" required>
          <el-input v-model="formInline.stuName" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄">
          <el-input v-model="formInline.stuAge" placeholder="年龄"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" plain @click="selectByCondition" icon="el-icon-search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="warning" plain @click="resetSelectForm('formInline')" icon="el-icon-refresh">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="success" plain @click="addStudentDialogVisible=true" icon="el-icon-circle-plus-outline">添加学生</el-button>
        </el-form-item>
      </el-form>
      <el-table
          class="table"
          :data="studentList.filter(data => !search || data.stuName.toLowerCase().includes(search.toLowerCase()))"
          style="width: 100%"
          stripe="stripe"
          :header-cell-style="{color:'#000'}">
        <el-table-column
            label="学号"
            prop="stuId"
            sortable>
        </el-table-column>
        <el-table-column
            label="姓名"
            prop="stuName">
        </el-table-column>
        <el-table-column
            label="年龄"
            prop="stuAge">
        </el-table-column>
        <el-table-column
            align="center">
          <template slot="header" slot-scope="scope">
            <el-input
                v-model="search"
                size="small"
                placeholder="输入关键字搜索"/>
          </template>
          <template slot-scope="scope">
            <el-button
                size="mini"
                @click="handleEdit(scope.$index, scope.row)"
                :style="{color:'#000' }">编辑</el-button>
            <el-button
                size="mini"
                type="danger"
                @click="handleDelete(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
<!--    分页-->
    <el-pagination
        align="right"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
    </el-pagination>

    <!-- 添加学生对话框 -->
    <el-dialog
      title="添加学生信息"
      :visible.sync="addStudentDialogVisible"
      width="40%"
      center
      :close-on-click-modal="false">
      <el-form :model="addMsgForm" :rules="rules" ref="addMsgForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="学生学号" prop="stuId" required>
          <el-input v-model="addMsgForm.stuId"></el-input>
        </el-form-item>        
        <el-form-item label="学生姓名" prop="stuName" required>
          <el-input v-model="addMsgForm.stuName"></el-input>
        </el-form-item>
        <el-form-item label="学生年龄" prop="stuAge" required>
          <el-input v-model="addMsgForm.stuAge"></el-input>
        </el-form-item>
        <el-form-item style="margin: 0 auto;">
          <el-button type="primary" @click="addStudent" plain>立即添加</el-button>
          <el-button type="warning" @click="resetForm('addMsgForm')" plain >重置</el-button>
          <el-button @click="addStudentDialogVisible = false">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import studentApi from '@/api/studentApi';
export default {
  name: "stuList",
  data() {
    return {
      msg: "这是list页面",
      search: '',  //表格中搜索框
      studentList: [],
      currentPage: 1,
      pageSize: 5,
      total: 20,
      addStudentDialogVisible:false,
      formInline: {
          stuId: '',
          stuName: '',
          stuAge:0
      },
      addMsgForm:{
        stuId:'',
        stuName:'',
        stuAge:0
      },
    }
  },
  created() {
    this.getList();
    this.selectByPage(this.currentPage, this.pageSize);
  },
  mounted() {

  },
  methods: {

    // 获取学生列表
     async getList() {

      let {data:res} = await studentApi.selectAll();
      console.log("查询所有数据")
      console.log(res)
    },

    // 分页
    async selectByPage(){
      let params={
        currentPage:this.currentPage,
        pageSize:this.pageSize
      }
      let res = await studentApi.selectByPage(params);
      this.studentList = res.data.rows;
      this.total=res.data.total;
    },

    // 条件查询
    async selectByCondition(){
      let res = await studentApi.selectByCondition(this.currentPage,this.pageSize, this.formInline);
      this.studentList=res.data.rows;
      this.total=res.data.total;
      console.log(res)
    },

    // 添加学生
    async addStudent(){
      let res = await studentApi.addStudent(this.addMsgForm);
      console.log(res)
      if(res.data.code==200){
        this.$message({
          type: 'success',
          message: '添加成功!'
        });
        this.addStudentDialogVisible=false;
        this.selectByPage();
      }else{
        this.$message({
          type: 'error',
          message: '添加失败!'
        });
      }
    },

    handleSizeChange(val) {
      this.pageSize = val;
      this.selectByPage();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.selectByPage();
    },
    resetSelectForm(){
      this.formInline = {
        stuId: '',
        stuName: '',
        stuAge:0
      };
      this.selectByPage()
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },

  }
}
</script>
<style scoped>
  *{
    box-sizing:border-box
  }
  #list{
    width:90%;
    margin: 0 auto;
  }
  h1{
    margin-bottom: 20px;
  }

  #list .table{

  }
  .el-pagination{
    margin : 20px;
  }
  .demo-form-inline{
    text-align: left;
  }
</style>